package gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.BorderLayout;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JSplitPane;
import net.miginfocom.swing.MigLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.util.Locale;
import javax.swing.AbstractAction;
import javax.swing.Action;
import java.awt.Component;
import java.awt.Dialog.ModalityType;
import javax.swing.border.MatteBorder;
import javax.swing.border.EmptyBorder;

import loc.messages;
import javax.swing.ImageIcon;
import java.awt.Toolkit;

public class GVOD {

	private JFrame frmGvod;
	private JTextField textField;
	public static String globalStatus = "";
	private final Action actionAddVideo = new SwingAction();
	private BrowseSources browseDialog;
	private JPanelMainContent panelMainContent;
	private JPanelDetails panelDetails;
	public static JLabel lblDown;
	private final Action actionRemoveVideo = new SwingAction_1();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					GVOD window = new GVOD();
					window.frmGvod.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public GVOD() {
		initialize();
	}

	public void resetText() {
		frmGvod.setTitle(Locale.getDefault().getLanguage());
	}
	
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmGvod = new JFrame();
		frmGvod.setIconImage(Toolkit.getDefaultToolkit().getImage(GVOD.class.getResource("/res/preferences-system-network.png")));
		frmGvod.setTitle(messages.getString("GVOD.frmGvod.title")); //$NON-NLS-1$ 
		frmGvod.setBounds(100, 100, 800, 600);
		frmGvod.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmGvod.setLocationRelativeTo(null); 
		
		try {
		    // Set System L&F
	        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	    } catch (UnsupportedLookAndFeelException e) {
	       // handle exception
	    } catch (ClassNotFoundException e) {
	       // handle exception
	    } catch (InstantiationException e) {
	       // handle exception
	    } catch (IllegalAccessException e) {
	       // handle exception
	    }
	    
		System.out.println(UIManager.getLookAndFeel());
		JMenuBar menuBar = new JMenuBar();
		frmGvod.setJMenuBar(menuBar);
		
		JMenu MenuFile = new JMenu(messages.getString("GVOD.MenuFile.text")); //$NON-NLS-1$ 
		MenuFile.setMnemonic('f');
		menuBar.add(MenuFile);
		
		JMenuItem MenuFileItemQuit = new JMenuItem(messages.getString("GVOD.MenuFileItemQuit.text")); //$NON-NLS-1$ 
		MenuFileItemQuit.setIcon(new ImageIcon(GVOD.class.getResource("/res/application-exit.png")));
		MenuFileItemQuit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("Application exited cleanly. Code #0.");
				System.exit(0);
			}
		});
		MenuFileItemQuit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK));
		MenuFile.add(MenuFileItemQuit);
		
		JMenu mnLanguage = new JMenu(messages.getString("GVOD.mnLanguage.text")); //$NON-NLS-1$ 
		menuBar.add(mnLanguage);
		
		JMenuItem mntmEnglish = new JMenuItem(messages.getString("GVOD.mntmEnglish.text")); //$NON-NLS-1$ 
		mntmEnglish.setIcon(new ImageIcon(GVOD.class.getResource("/loc/en_US.png")));
		mntmEnglish.setEnabled(false);
		
		JMenuItem mntmDebugSysoutDefault = new JMenuItem(messages.getString("GVOD.mntmDebugSysoutDefault.text")); //$NON-NLS-1$ 
		mntmDebugSysoutDefault.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Locale.setDefault(Locale.ENGLISH);
				resetText();
				System.out.println(Locale.getDefault());
			}
		});
		mnLanguage.add(mntmDebugSysoutDefault);
		mnLanguage.add(mntmEnglish);
		
		JMenuItem mntmNorwegian = new JMenuItem(messages.getString("GVOD.mntmNorwegian.text"));
		mntmNorwegian.setIcon(new ImageIcon(GVOD.class.getResource("/loc/no_NO.png")));
		mntmNorwegian.setEnabled(false);
		mnLanguage.add(mntmNorwegian);
		
		JMenu mnAbout = new JMenu(messages.getString("GVOD.mnAbout.text")); //$NON-NLS-1$
		menuBar.add(mnAbout);
		
		JMenuItem mntmAbout = new JMenuItem(messages.getString("GVOD.mntmAbout.text")); //$NON-NLS-1$
		mntmAbout.setIcon(new ImageIcon(GVOD.class.getResource("/res/gtk-info.png")));
		mnAbout.add(mntmAbout);
		
		JPanel toolBar = new JPanel();
		toolBar.setBackground(UIManager.getColor("ToolBar.background"));
		toolBar.setBorder(new MatteBorder(0, 0, 1, 0, UIManager.getColor("ToolBar.darkShadow")));
		frmGvod.getContentPane().add(toolBar, BorderLayout.NORTH);
		toolBar.setLayout(new MigLayout("", "[left][left][left][grow,fill][right][right]", "[fill]"));
		
		JButton btnDownloadVideos = new JButton();
		btnDownloadVideos.setBorderPainted(false);
		btnDownloadVideos.setIcon(new ImageIcon(GVOD.class.getResource("/res/document-save.png")));
		toolBar.add(btnDownloadVideos, "cell 0 0");
		
		JButton btnAddVideos = new JButton();
		btnAddVideos.setBorderPainted(false);
		btnAddVideos.setIcon(new ImageIcon(GVOD.class.getResource("/res/document-properties.png")));
		btnAddVideos.setAction(actionAddVideo);
		toolBar.add(btnAddVideos, "cell 1 0");
		
		JButton btnRemoveVideos = new JButton(messages.getString("GVOD.btnRemoveVideos.text"));
		btnRemoveVideos.setAction(actionRemoveVideo);
		toolBar.add(btnRemoveVideos, "cell 2 0");
		
		JLabel lblFilter = new JLabel(messages.getString("GVOD.lblFilter.text")); //$NON-NLS-1$ 
		lblFilter.setAlignmentX(Component.RIGHT_ALIGNMENT);
		lblFilter.setHorizontalAlignment(SwingConstants.CENTER);
		toolBar.add(lblFilter, "cell 4 0,alignx right,aligny center");
		
		panelMainContent = new JPanelMainContent();
		panelMainContent.setBorder(new EmptyBorder(7, 7, 4, 7));
		panelMainContent.setBackground(UIManager.getColor("window"));
		panelDetails = new JPanelDetails();
		JPanelMainContent.getVideoList().getSelectionModel().addListSelectionListener(panelDetails);
		
		textField = new JTextField();
		textField.setAlignmentX(Component.RIGHT_ALIGNMENT);
		textField.setHorizontalAlignment(SwingConstants.LEFT);
		textField.setColumns(30);
		textField.addKeyListener(panelMainContent);
		
		lblFilter.setLabelFor(textField);
		toolBar.add(textField, "cell 5 0,alignx right,aligny center");
		
		JSplitPane splitPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panelMainContent, panelDetails);
		splitPanel.setBorder(null);
		splitPanel.setResizeWeight(1.0);
		frmGvod.getContentPane().add(splitPanel, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		panel.setBorder(new MatteBorder(1, 0, 0, 0, UIManager.getColor("List.dropLineColor")));
		frmGvod.getContentPane().add(panel, BorderLayout.SOUTH);
		panel.setLayout(new MigLayout("", "[left][grow,fill][right][right]", "[]"));
		
		JLabel lblStatus = new JLabel(messages.getString("GVOD.lblStatus.text")); //$NON-NLS-1$ 
		panel.add(lblStatus, "cell 0 0,alignx left,aligny top");
		
		JLabel lblTotal = new JLabel(messages.getString("GVOD.lblTotal.text")); //$NON-NLS-1$ 
		panel.add(lblTotal, "cell 2 0");
		
		lblDown = new JLabel(messages.getString("GVOD.lblDown.text")); //$NON-NLS-1$ 
		panel.add(lblDown, "cell 3 0");
		
	}
	
	public static void setGlobalStatusString(String string) {
		globalStatus = string;
		System.out.println(string);
	}
	
	public static void setTotalSpeedString(String string) {
		lblDown.setText(string + " KiB/s");
	}
	
	private class SwingAction extends AbstractAction {
		/**
		 * 
		 */
		private static final long serialVersionUID = -3903366531607736488L;
		public SwingAction() {
			putValue(Action.LARGE_ICON_KEY, new ImageIcon(GVOD.class.getResource("/res/document-properties.png")));
			putValue(NAME, "");
			putValue(SHORT_DESCRIPTION, "Add video(s) for download.");
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			if(browseDialog == null) {
				browseDialog = new BrowseSources();
				browseDialog.setModalityType(ModalityType.APPLICATION_MODAL);
				//TODO: temp. modality bugfix...
				browseDialog.setVisible(false);
			}
			browseDialog.setVisible(true);
			browseDialog.checkVideos();
		}
	}
	private class SwingAction_1 extends AbstractAction {
		/**
		 * 
		 */
		private static final long serialVersionUID = -5400779252407517620L;
		public SwingAction_1() {
			putValue(NAME, messages.getString("GVOD.btnRemoveVideos.text"));
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		@Override
		public void actionPerformed(ActionEvent e) {
//			int[] selectedRows = JPanelMainContent.getVideoList().getSelectedRows();
//			JPanelMainContent.getVideoList().removeVideoAtIndexes(selectedRows);
		}
	}
}
